package qc.module.sl.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.module.sl.dto.project.ProjectDto;
import qc.module.sl.dto.project.ProjectQueryConditionDto;
import qc.module.sl.entity.SlBasicProjects;
import qc.module.sl.mapper.BasicProjectsMapper;
import qc.module.sl.repository.SlBasicProjectsRepository;

import java.util.List;

/**
 * BasicProjectsService
 *
 * @author QuCheng Tech
 * @create 2023/02/14
 */
@Service
public class BasicProjectsService {
    private SlBasicProjectsRepository repository;
    
    @Autowired
    public void setSlBasicProjectsRepository(SlBasicProjectsRepository repository){
        this.repository = repository;
    }
    
    private BasicProjectRelationService basicProjectRelationService;
    
    @Autowired
    public void setBasicProjectRelationService(BasicProjectRelationService basicProjectRelationService){
        this.basicProjectRelationService = basicProjectRelationService;
    }

    /**
     * 根据查询条件查询项目信息
     *
     * @param dto 项目信息查询dto
     * @return 项目信息集合
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    public List<ProjectDto> query(ProjectQueryConditionDto dto) throws Exception {
        LambdaQueryWrapper<SlBasicProjects> wrapper = new LambdaQueryWrapper<>();

        //指定查询字段，默认不查询边界信息
        wrapper.select(SlBasicProjects::getId, SlBasicProjects::getName, SlBasicProjects::getAdcode, SlBasicProjects::getDescription, SlBasicProjects::getLng, SlBasicProjects::getLat, SlBasicProjects::getEnable);

        if (dto != null) {
            //需要查询边界信息，查询全部字段
            if (dto.isIncludegeo())
                wrapper.select(SlBasicProjects::getId, SlBasicProjects::getName, SlBasicProjects::getAdcode, SlBasicProjects::getDescription, SlBasicProjects::getLng, SlBasicProjects::getLat, SlBasicProjects::getEnable, SlBasicProjects::getGeos);

            if (!StringUtils.isBlank(dto.getClasscode())) {
                List<Integer> list = basicProjectRelationService.queryProjectIdByClassifyCode(dto.getClasscode());
                if (list != null)
                    wrapper.in(SlBasicProjects::getId, list);
            }
            if (!StringUtils.isBlank(dto.getStatus()))
                if (dto.getStatus().equals("0") || dto.getStatus().equals("1"))
                    wrapper.eq(SlBasicProjects::getEnable, dto.getStatus());
                
            if (!StringUtils.isBlank(dto.getWords()))
                wrapper.and(k -> k.like(SlBasicProjects::getName, dto.getWords()).or().like(SlBasicProjects::getDescription, dto.getWords()));
        }
        //按照工程名称升序排序
        wrapper.orderByAsc(SlBasicProjects::getName);

        List<SlBasicProjects> result = repository.selectList(wrapper);

        if (result.size() > 0x0)
            return BasicProjectsMapper.MAPPER.toDtoList(result);

        return null;
    }

}
